package com.sample.user.service;

import org.apache.dubbo.config.annotation.DubboReference;

import com.sample.user.entity.User;
import com.swak.annotation.Get;
import com.swak.annotation.Post;
import com.swak.annotation.RestService;
import com.swak.annotation.Tcc;
import com.swak.entity.Result;

/**
 * 用户服务： 这种简单模式的使用
 * 
 * @author DELL
 */
@RestService(path = "/user2")
public class UserSer {

	@Tcc
	@DubboReference(sticky = true, group = "sj", stub = "true")
	private UserService userService;

	/**
	 * 获取用户
	 * 
	 * @return
	 */
	@Get("/get/sync")
	public Result getSync() {
		User user = userService.get(2L);
		return Result.success(user);
	}

	/**
	 * 开启全局事务
	 * 
	 * @return
	 */
	@Tcc(confirm = "transConfirm", cancel = "transCancel")
	@Post("/trans")
	public Result trans(String orderId) {
		System.out.println("==============================================");
		System.out.println("============    准备阶段                      ================");
		System.out.println("==============================================");

		// 执行分布式事务
		this.userService.doTrans(orderId);

		// 返回结果
		return Result.success();
	}

	/**
	 * 事务提交
	 * 
	 * @return
	 */
	public void transConfirm(String orderId) {
		System.out.println("==============================================");
		System.out.println("============    确认阶段                      ================");
		System.out.println("==============================================");
	}

	/**
	 * 事务取消
	 * 
	 * @return
	 */
	public void transCancel(String orderId) {
		System.out.println("==============================================");
		System.out.println("============    取消阶段                      ================");
		System.out.println("==============================================");
	}
}
